package com.pactera.mavenweb.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pactera.mavenweb.dto.Page;
import com.pactera.mavenweb.entity.Book;
import com.pactera.mavenweb.service.BookService;
import com.pactera.mavenweb.vo.BookVo;

@Controller
@RequestMapping("/book")
public class MyAjaxController {
	
	@Resource(name="bookService")
	private BookService bookService;
	
	@RequestMapping("/getAllBook")
	public String transformToManagement() {
		return "book_management";
	}
	
	@RequestMapping("/jqgrid")
	public String transformToBook() {
		
		return "jqgrid";
	}
	
	@RequestMapping(value="/getAllBookByPage", params="method=getAllBookByPage",method=RequestMethod.GET)
	public @ResponseBody
	Map<String, Object> getAllBookByPage(HttpServletRequest request, HttpServletResponse response) throws Exception { 
		Page page = new Page();
		
		int currentPage = Integer.valueOf(request.getParameter("page.page"));
		int maxPage = Integer.valueOf(request.getParameter("page.num"));
		
		page.setNum(maxPage);
		
		int refreshedMaxPage = bookService.getAmountForAllBook();
		page.setMax(refreshedMaxPage);
		
		// if we are asking for a page that doesn't exist any longer; jump to page 1.
		if(page.getSize() < currentPage) {
			page.setPage(1);
		} else {
			page.setPage(currentPage);
		}
		
		System.out.println("refreshedMaxPage = " + refreshedMaxPage + ", page.getPage() = " + page.getPage());
		
		List<Book> bookList = bookService.getAllBookByPageOrder(page);
		int size = bookList.size();
		
		Book[] bookArray = new Book[size];
		Book tempBook = null;
		for(int i = 0; i < size; i ++) {
			tempBook = bookList.get(i);
			bookArray[i] = new Book(tempBook.getId(),
									tempBook.getName(),
									tempBook.isBorrowed(),
									tempBook.getRemarks());
		}
		
		BookVo bookVo = new BookVo();
		bookVo.setPage(page);
		bookVo.setBookArray(bookArray);
				
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("bookVo", bookVo);
		return map;
	}
	
	@RequestMapping(value="/updateBook", params="method=updateBook", method=RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> updateBook(HttpServletRequest request, HttpServletResponse response) throws Exception { 
		Book book = new Book();
		
		book.setId(Integer.valueOf(request.getParameter("book.id")));
		book.setName(request.getParameter("book.name"));
		book.setBorrowed(Boolean.valueOf(request.getParameter("book.borrowed")));
		book.setRemarks(request.getParameter("book.remarks"));
		
		bookService.updateBook(book);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("msg", "success");
		return map;
	}
	
	@RequestMapping(value="/updateBook", params="method=deleteBookById", method=RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> deleteBook(HttpServletRequest request, HttpServletResponse response) throws Exception { 
		Book book = new Book();
		BookVo bookVo = new BookVo();
		Map<String, Object> map = new HashMap<String, Object>();
		
		book.setId(Integer.valueOf(request.getParameter("book.id")));
		
		if (!bookService.delBook(book.getId())) {
			
			Page page = new Page();
			page.setMax(bookService.getAmountForAllBook());

			bookVo.setPage(page);
			bookVo.setMsg("error");
			
			map.put("bookVo", bookVo);
			return map;
		}
		
		map.put("msg", "success");
		bookVo.setMsg("success");
		map.put("bookVo", bookVo);
		return map;
	}
	
	@RequestMapping(value="/addBook", params="method=addBook", method=RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> addBook(HttpServletRequest request, HttpServletResponse response) throws Exception { 
		Book book = new Book();
		
		book.setId(Integer.valueOf(request.getParameter("book.id")));
		book.setName(request.getParameter("book.name"));
		book.setBorrowed(Boolean.valueOf(request.getParameter("book.borrowed")));
		book.setRemarks(request.getParameter("book.remarks"));
		
		bookService.addBook(book);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("msg", "success");
		return map;
	}
}
